﻿using System;
using CzfSDK;
using System.Collections;
using System.Data;
using System.Collections.Generic;

namespace Server
{
	public static class MConfig
	{
		const int timeout = 59; // 数据过期时间(秒)

		// 强制更新
		public static void Update()
		{
			timeGameConfig = new DateTime(0);
			GetGameConfigHt ();

			timeSkillTemplate = new DateTime(0);
			GetSkillTemplate ();

			timeSkillArrow = new DateTime(0);
			GetSkillArrow ();

			timeSkillServant = new DateTime(0);
			GetSkillServant ();

			timeSkillAffix = new DateTime(0);
			GetSkillAffix ();

			timeRoleTemplate = new DateTime(0);
			GetRoleTemplate ();

			timeItemTemplate = new DateTime(0);
			GetItemTemplate ();

			timeArenaTemplate = new DateTime(0);
			GetArenaTemplate ();
			ArenaManager.CheckLvAdd ();
		}

		#region SkillAffix
		private static Dictionary<string, Hashtable> skillAffix = null;
		private static DateTime timeSkillAffix;
		public static Dictionary<string, Hashtable> GetSkillAffix()
		{
			if (skillAffix == null || (DateTime.Now - timeSkillAffix).TotalSeconds > timeout) {
				string sql = "select * from skillaffix";
				var dt = MysqlPool.current.ExecDataTable (sql);
				skillAffix = new Dictionary<string, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					skillAffix [dr ["id"].ToString()] = dr.ToHashtable ();
				}
				timeSkillAffix = DateTime.Now;
			}

			return skillAffix;
		}
		#endregion

		#region GameConfig
		private static Hashtable htGameConfig = null;
		private static DateTime timeGameConfig;
		public static Hashtable GetGameConfigHt()
		{
			if (htGameConfig == null || (DateTime.Now - timeGameConfig).TotalSeconds > timeout) {
				string sql = "select * from gamepara";
				var dt = MysqlPool.current.ExecDataTable (sql);
				htGameConfig = new Hashtable (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					htGameConfig [(string)dr ["key"]] = dr ["value"];
				}
				timeGameConfig = DateTime.Now;
			}
			return htGameConfig;
		}

		public static T GetGameConfig<T>(string key)
		{
			var ht = GetGameConfigHt ();
			return ht.GetVal<T> (key);
		}
		#endregion

		#region SkillTemplate
		private static Dictionary<int, Hashtable> skillTemplate = null;
		private static DateTime timeSkillTemplate;
		public static Dictionary<int, Hashtable> GetSkillTemplate()
		{
			if (skillTemplate == null || (DateTime.Now - timeSkillTemplate).TotalSeconds > timeout) {
				string sql = "select * from skilltemp";
				var dt = MysqlPool.current.ExecDataTable (sql);
				skillTemplate = new Dictionary<int, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					skillTemplate [Convert.ToInt32(dr ["id"])] = dr.ToHashtable ();
				}
				timeSkillTemplate = DateTime.Now;
			}

			return skillTemplate;
		}

		public static Hashtable GetSkillConfig(int skillId)
		{
			var temp = GetSkillTemplate ();
			return temp[skillId];
		}
		#endregion

		#region SkillArrow
		private static Dictionary<string, Hashtable> skillArrow = null;
		private static DateTime timeSkillArrow;
		public static Dictionary<string, Hashtable> GetSkillArrow()
		{
			if (skillArrow == null || (DateTime.Now - timeSkillArrow).TotalSeconds > timeout) {
				string sql = "select * from skillarrow";
				var dt = MysqlPool.current.ExecDataTable (sql);
				skillArrow = new Dictionary<string, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					skillArrow [dr ["id"].ToString()] = dr.ToHashtable ();
				}
				timeSkillArrow = DateTime.Now;
			}

			return skillArrow;
		}
		#endregion

		#region SkillServant
		private static Dictionary<string, Hashtable> skillServant = null;
		private static DateTime timeSkillServant;
		public static Dictionary<string, Hashtable> GetSkillServant()
		{
			if (skillServant == null || (DateTime.Now - timeSkillServant).TotalSeconds > timeout) {
				string sql = "select * from skillservant";
				var dt = MysqlPool.current.ExecDataTable (sql);
				skillServant = new Dictionary<string, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					skillServant [dr ["id"].ToString()] = dr.ToHashtable ();
				}
				timeSkillServant = DateTime.Now;
			}

			return skillServant;
		}
		#endregion

		#region RoleTemplate
		private static Dictionary<int, Hashtable> roleTemplate = null;
		private static DateTime timeRoleTemplate;
		public static Dictionary<int, Hashtable> GetRoleTemplate()
		{
			if (roleTemplate == null || (DateTime.Now - timeRoleTemplate).TotalSeconds > timeout) {
				string sql = "select * from roletemplate";
				var dt = MysqlPool.current.ExecDataTable (sql);
				roleTemplate = new Dictionary<int, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					roleTemplate [Convert.ToInt32 (dr ["id"])] = dr.ToHashtable ();
				}
				timeRoleTemplate = DateTime.Now;
			}

			return roleTemplate;
		}

		public static Hashtable GetRoleConfig(int roleId)
		{
			var temp = GetRoleTemplate ();
			if(temp.ContainsKey(roleId))
				return temp[roleId];

			return null;
		}
		#endregion

		#region ItemTemplate
		private static Dictionary<int, Hashtable> itemTemplate = null;
		private static DateTime timeItemTemplate;
		public static Dictionary<int, Hashtable> GetItemTemplate()
		{
			if (itemTemplate == null || (DateTime.Now - timeItemTemplate).TotalSeconds > timeout) {
				string sql = "select * from itemtemplate";
				var dt = MysqlPool.current.ExecDataTable (sql);
				itemTemplate = new Dictionary<int, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					itemTemplate [Convert.ToInt32(dr ["id"])] = dr.ToHashtable ();
				}
				timeItemTemplate = DateTime.Now;
			}

			return itemTemplate;
		}
		#endregion
	
		#region Arenatemplate
		private static Dictionary<int, Hashtable> arenaTemplate = null;
		private static DateTime timeArenaTemplate;
		public static Dictionary<int, Hashtable> GetArenaTemplate()
		{
			if (arenaTemplate == null || (DateTime.Now - timeArenaTemplate).TotalSeconds > timeout) {
				string sql = "select * from arenatemplate";
				var dt = MysqlPool.current.ExecDataTable (sql);
				arenaTemplate = new Dictionary<int, Hashtable> (dt.Rows.Count);
				foreach (DataRow dr in dt.Rows) {
					arenaTemplate [Convert.ToInt32(dr ["id"])] = dr.ToHashtable ();
				}
				timeArenaTemplate = DateTime.Now;
			}

			return arenaTemplate;
		}
		#endregion
	}
}

